/*
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-12-12 15:41:45
 * @LastEditors: Andy
 * @LastEditTime: 2023-01-13 18:30:52
 */
const getAssetsFile = function (url) {
  // 获取文件夹下所有图片名称
  const files = import.meta.glob(['../assets/images/*.png', '../assets/images/*.jpg'], { eager: true })
  const AllPics = Object.values(files).map((v) => v.default)
  let isExist = 0
  let imgSrc = ''
  // 遍历所有图片，判断是否存在该url图片
  AllPics.forEach((val) => {
    const tempUrl = url.substring(0, url.indexOf('.'))
    console.log('tempUrl===============', tempUrl, val)
    if (val.indexOf(tempUrl) !== -1) {
      isExist += 1
    }
  })
  if (isExist !== 0) {
    // 头像存在
    imgSrc = url
  } else {
    imgSrc = 'defaultavater.png'
  }

  return new URL(`./images/${imgSrc}`, import.meta.url).href
}

export default getAssetsFile
